import axios from 'axios'
import { getToken } from '@/utils/auth'

const mimeMap = {
  xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  zip: 'application/zip'
}

// const baseUrl = process.env.VUE_APP_BASE_API
const baseUrl = "http://miraclepeak.xyz:8888/subject/"

/**
 * 下载zip
 *
 * @param str
 * @param fileName
 */
export function downLoadZip(str, fileName) {
  var url = baseUrl + str
  axios({
    method: 'get',
    url: url,
    responseType: 'blob',
    headers: { 'Authorization': 'Bearer ' + getToken() }
  }).then(res => {
    resolveBlob(res, mimeMap.zip,fileName)
  })
}
/**
 * 解析blob响应内容并下载
 * @param {*} res blob响应内容
 * @param {String} mimeType MIME类型
 */
export function resolveBlob(res, mimeType, fileName) {
  const aLink = document.createElement('a')
  var blob = new Blob([res.data], { type: mimeType })
  aLink.href = URL.createObjectURL(blob)
  aLink.setAttribute('download', fileName) //  改造一下，下载的文件名前端来定
  document.body.appendChild(aLink)
  aLink.click()
  document.body.appendChild(aLink)
}
